/// <reference path="../_all.ts" />

module Application.Services {

    export class TranslationService implements Application.Interfaces.Services.ITranslationService {

        private _translation: Application.Resources.ITranslationClass;

        public static Translations: any;

        public CurrentTranslation: any;

        private _defaults: any;

        private _translations: any;

        public static $inject = [
            '$resource',
            '$cookies',
            '$cookieStore',
        ];

        constructor(public Resource: ng.resource.IResourceService, public Cookies: ng.cookies.ICookiesService, public CookieStore: ng.cookies.ICookieStoreService) {
            this._translation = <Application.Resources.ITranslationClass> this.Resource(Application.Services.ServiceUtility.GetLangURL(':lang'), null, {
                getFile: { method: 'GET', cache: false, isArray: false }
            });

            this._translations = {};

            this._defaults = {
                "LOGIN": "Login",
                "SERVERLIST": "Servers List",
                "SHORTCUTS": "Shortcuts",
                "REFRESH": "Refresh",
                "NAME": "Name",
                "STATUS": "Status",
                "LASTDATETIME": "Last Datetime",
                "ACTIONS": "Actions",
                "RUNNING": "Running",
                "STOPPED": "Stopped",
                "UPDATING": "Updating",
                "STOPPING": "Stopping",
                "STARTING": "Starting",
                "MANAGE": "Manage",
                "LASTACTION": "Last Action",
                "TEMPLATEPATH": "Template Path",
                "CONFIGPATH": "Config Path",
                "SERVERPATH": "Server Path",
                "EXECUTABLE": "Executable",
                "PORTNUMBER": "Port Number",
                "START": "Start",
                "STOP": "Stop",
                "UPDATE": "Update",
                "SERVERINFO": "Server Information",
                "SERVERID": "Server Id",
                "SERVERNAME": "Server Name",
                "SERVERCONFIG": "Server Configuration",
                "APPLY": "Apply",
                "RESET": "Reset",
                "NEWCONFIG": "New Configuration",
                "NEWCONFIGNAME": "Configuration Name",
                "NEWCONFIGVAL": "Configuration Value",
                "SERVER": "Server",
                "PLAYERS": "Players",
                "SERVERACTIONS": "Server Actions",
                "SERVERRIGHTS": "Server Rights",
                "USERS": "Users",
                "ACCOUNT": "Account",
                "LOGOUT": "Log Out",
                "USERLOGGEDIN": "User {0} logged in",
                "USER": "User",
                "ACTION": "Action",
                "CREATEDAT": "Created At",
                "EXECUTEDENDED": "Ended At",
                "CONNECTION": "Connection",
                "PASSWORD": "Password",
                "CONNECTWITHCREDENTIALS": "Connect with your credentials",
                "MEMBERS": "Members",
                "FILTERMEMBERS": "Filter Members",
                "GAMEID": "Game ID",
                "RANK": "Ranks",
                "RIGHTS": "Rights",
                "RELOAD": "Reload",
                "FILTERINGCRITERIAS": "Filtering Criterias",
                "PLAYERNAMES": "Player Names",
                "SERVERS": "Servers",
                "PLAYERNAME": "Player Name",
                "SERVERID": "Server ID",
                "LOADINGCRITERIAS": "Loading Criterias",
                "PLAYERGUID": "Player GUID",
                "FILTERBANNED": "Banned (0/1)",
                "RESETFILTERS": "Reset Filters",
                "GUID": "Guid",
                "CONNECTED": "Connected",
                "IP": "IP",
                "BANREASON": "Ban reason",
                "LIFTBAN": "Lift the ban",
                "CHANGEMYPROFILE": "Change my profile",
                "OLDPASSWORD": "Old Password",
                "NEWPASSWORD": "New Password",
                "NEWPASSWORD2": "New Password (repeat)",
                "EMAIL": "Email",
                "SAVE": "Save",
                "FILTERUSERS": "Filter users",
                "ROLE": "Role",
                "ACTIVE": "Active",
                "LASTIP": "Last IP",
                "LASTCONNECTION": "Last Connection",
                "NEWUSER": "New User",
                "ADDUSER": "Add User",
                "FOUNDUSERS": "{0} users found",
                "USERNOTFOUND": "User NOT found",
                "USERADDEDWITHPASS": "User {0} added with password {1}",
                "USERUPDATED": "User {0} was updated",
                "UNKNOWNERROR": "An unknown error occured",
                "TWOUSERSSAMENAME": "You cannot have two users with the same name",
                "NOROLEHIGHER": "No role higher",
                "NOROLELOWER": "No role lower",
                "USERSNOTFOUND": "Users NOT found",
                "INVALID": "Invalid",
                "INVALIDNOCHANGES": "You didn't made any changes",
                "PASSWORDTOOSHORT": "Your password is too short. It should be at least 6 characters long",
                "FILLYOURPASSWORD": "You must fill in your old password",
                "RETYPEPASSWORD": "You must type your new password and repeat it",
                "SERVERSFOUND": "{0} servers found",
                "SERVERSNOTFOUND": "No servers found!",
                "NOSERVERID": "No server ID",
                "SERVERINFOLOADED": "The server info are loaded",
                "ALLCHANGESSAVED": "All the changes have been successfully saved!",
                "ERRORSAVINGCHANGES": "There was an error while saving your changes!",
                "CONFIGNOTDELETED": "The configuration could not be deleted",
                "CONFIGDELETED": "The configuration {0} ({1}) has been deleted!",
                "CONFIGADDED": "The configuration {0} has been added!",
                "CONFIGNOTADDED": "The configuration has not been added!",
                "SERVERNOTFOUND": "Server not found",
                "ACTIONQUEUED": "The action has been successfully queued!",
                "ACTIONFAILED": "Failed to add the action!",
                "NOPLAYERSFOUND": "No players found",
                "NOPLAYERUPDATED": "No player updated",
                "PLAYERUPDATED": "Player {0} updated!",
                "PLAYERSUPDATED": "{0} players updated!",
                "PLAYERSFOUND": "{0} players found!",
                "NOMEMBERSFOUND": "No members found!",
                "MEMBERSFOUND": "{0} members found!",
                "ACTIONSNOTFOUND": "No actions found!",
                "INVALIDACTION": "Invalid Action",
                "ACTIONSFOUND": "{0} actions found",
                "INTERACTIVECONSOLE": "Interactive Console",
                "CONNECT": "Connect",
                "DISCONNECT": "Disconnect",
                "LANGUAGE": "Language",
           };
        }

        public Translate(text: string): string {
            if (Application.Services.TranslationService.Translations && Application.Services.TranslationService.Translations.hasOwnProperty(text)) {
                return Application.Services.TranslationService.Translations[text];
            } else if (this._defaults.hasOwnProperty(text)) {
                return this._defaults[text];
            };
            return text;
        }

        public GetTranslation(language: string, callback: Function) {
            var path = Application.Constants.Services.LanguageRemoteUrl + language + '.json';
            var store = this._translations;
            if (store[language]) {
                Application.Services.TranslationService.Translations = store[language];
            } else {
                this._translation.getFile({ lang: language + '.json' },
                    function (data) {
                        store[language] = data;
                        Application.Services.TranslationService.Translations = data;
                        callback(data);
                    },
                    function (err) {
                        callback(null, err);
                    });
            }
        }
    };
}